package com.wnxy.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wnxy.admin.dto.CarDto;
import com.wnxy.admin.entity.Car;
import com.wnxy.admin.mapper.CarMapper;
import com.wnxy.admin.service.ICarService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 车辆表 服务实现类
 * </p>
 *
 * @author All
 * @since 2023-11-29
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {

    private final CarMapper carMapper;

    public CarServiceImpl(CarMapper carMapper) {
        this.carMapper = carMapper;
    }


    @Override
    public List<Car> getCarByCustomerId(Integer customerId) {
      return carMapper.selectList(Wrappers.lambdaQuery(Car.class)
                .eq(Car::getCustomerId,customerId));
    }

    @Override
    public PageInfo<CarDto> getAll(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        List<CarDto> carDtos = carMapper.selectAll(keyword);
        return new PageInfo<>(carDtos);
    }

    @Override
    public PageInfo<CarDto> cGetAll(Integer pageNum, Integer pageSize, String keyword, Integer id) {
        PageHelper.startPage(pageNum, pageSize);
        List<CarDto> carDtos = carMapper.cSelectAll(keyword,id);
        return new PageInfo<>(carDtos);
    }

    @Override
    public Integer getCountById(Integer customerId) {
        Long aLong = carMapper.selectCount(Wrappers.lambdaQuery(Car.class)
                .eq(Car::getCustomerId, customerId)
        );
        String s = aLong.toString();
        return Integer.parseInt(s);
    }
}
